<?php

/***
 * Class UserController
 * 平台用户信息
 */
class UserController extends ManageController{

    function init(){
        parent::init();
    }

    #用户列表
    function indexAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['mobile'] = !empty($_REQUEST['mobile']) ? trim($_REQUEST['mobile']) : '';
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['ip'] = !empty($_REQUEST['ip']) ? trim($_REQUEST['ip']) : '';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';
        $condition['ban'] = isset($_REQUEST['ban']) ? intval($_REQUEST['ban']) : '0';
        $condition['frozen'] = isset($_REQUEST['frozen']) ? intval($_REQUEST['frozen']) : '0';
        $condition['address'] = isset($_REQUEST['address']) ? trim($_REQUEST['address']) : '';

        $page_size = 20;
        $user_model = new UserModel();
        $data = $user_model->getListData($page, $page_size, $condition);


        #分页处理
        $totalnum = $user_model->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '注册用户列表';
    }


    #编辑用户信息
    function editUserAction()
    {
        $uid = isset($_REQUEST['uid']) ? intval($_REQUEST['uid']) : 0;
        $data['mobile'] = isset($_REQUEST['mobile']) ? intval($_REQUEST['mobile']) : 0;
        $reset_qrcode = !empty($_REQUEST['reset_qrcode']) ? intval($_REQUEST['reset_qrcode']) : 0;
        $change = !empty($_REQUEST['change']) ? trim($_REQUEST['change']) : 0;  //修改余额
        $memo = !empty($_REQUEST['memo']) ? trim($_REQUEST['memo']) : '';  //修改余额对应的备注
        $data['weixin'] = isset($_REQUEST['weixin']) ? trim($_REQUEST['memo']) : ''; //是否解绑微信
        $data['frozen'] = isset($_REQUEST['clear_frozen']) ? '0' : '1'; //是否解冻
        $data['udid'] = isset($_REQUEST['udid']) ? trim($_REQUEST['udid']) : '';
        $data['old_flag'] = isset($_REQUEST['old_flag']) ? intval($_REQUEST['old_flag']) : '0';

        if (empty($uid)) {
            $this->setError('参数错误', '/manage/user/index');
        }

        $user_model = new UserModel();
        $user_info = $user_model->getUserInfoByUid($uid);
        if (empty($user_info)) {
            $this->setError('参数错误', '/manage/user/index');
        }
        if ($this->getRequest()->isPost()) {
            #判断管理账户修改额度
            $login_user = $this->_view->user;
            if (!empty($change)) {
                if( $change > $login_user['limit_modify']) {
                    $this->setError("您的账号额度已用完，请联系技术", '/manage/user/index/?uid=' . $user_info['uid']);
                }
                #修改金币余额
                $data['balance'] = $user_info['balance'] + $change;
                $data['add_up'] = $user_info['add_up'] + $change;
                $exchange_obj = new UserExchangeModel();
                $memo = empty($memo) ? "后台管理员修改，变更值：" . $change : $memo;
                $exadd = array(
                    'uid' => $user_info['uid'],
                    'type' => 'admin',
                    'money' => $change,
                    'remain' => $user_info['balance'] + $change,
                    'created_at' => time(),
                    'memo' => $memo,
                    'pic' => UserExchangeModel::BOOKS_DEAULT_ICON,
                    'data_id' => $login_user['uid'],
                );
                $exchange_obj->addData($exadd);
            }

            $user_model->updateData($data, $user_info['uid']);

            if($reset_qrcode && !empty($user_info['qrcode'] )) {
                $this->_user_model->deleteFromFileToQiniu($user_info['qrcode']);
                $this->_user_model->genQrcodePic($user_info['uid']);
            }

            //同步微信绑定表
            if ($user_info['weixin'] != '' && $data['weixin'] == '') {
                $weixin_code_obj = new WeixinCodeModel();
                $updata = array(
                    'uid' => 0
                );
                $weixin_code_obj->updateData($updata, $user_info['uid']);  //清除原uid绑定信息
            }

            #记录操作历史
            $admin_action_model = new AdminActionHistoryModel();
            $addArray = array('type' => 'user', 'uid' => $this->_view->user['uid'], 'created_at' => time(), );
            if (!empty($change)) {
                $addArray['memo'] = "修改余额UID={$user_info['uid']} 变更" .$change;
            } else {
                $addArray['memo'] = "修改用户资料UID={$user_info['uid']}";
            }
            $admin_action_model->addData($addArray);
            if ($change > 0) {
                $updAdmin = array(
                    'limit_modify' => $login_user['limit_modify'] - $change,
                );
                $admin_user_model = new AdminUserModel();
                $admin_user_model->updateData($updAdmin, $login_user['uid']);
            }
            $this->setError("修改用户资料成功", '/manage/user/index/?uid=' . $user_info['uid']);
            return FALSE;
        }
        $this->_view->user_info = $user_info;
        $this->_layout->meta_title = '修改用户资料';
    }

    #任务参与历史记录
    function applyListAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $user_apply_history_model = new UserApplyHistoryModel();

        $condition = array();
        $condition['status'] = !empty($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['app_id'] = !empty($_REQUEST['app_id']) ? intval($_REQUEST['app_id']) : '0';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';
        $page_size = 20;

        $data = $user_apply_history_model->getListData($page, $page_size, $condition);
        $totalnum = $user_apply_history_model->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '活动参与历史记录';
    }

    #任务参与历史记录导出
    function applyListOutputAction()
    {
        $condition = array();
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : strtotime(mktime(0, 0, 0, date("m"), date("d"), date("Y")));
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : time();

        $condition = array();
        $condition['status'] = !empty($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['app_id'] = !empty($_REQUEST['app_id']) ? intval($_REQUEST['app_id']) : '0';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? $_REQUEST['start_date'] : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? $_REQUEST['end_date'] : '';

        $page_size = 200000;

        $user_apply_model = new UserApplyHistoryModel();
        $data = $user_apply_model->getListData(1, $page_size, $condition);

        $title = array("用户", "任务ID", "完成状态", "任务时间", "IP", "IDFA", "IOS版本");
        $showlist = array();
        foreach ($data as $k => $row) {
            $idfa = !empty($row['idfa']) ? $row['idfa'] : '';
            $ios_version = !empty($row['ios_version']) ? $row['ios_version'] : '';
            $showlist[] = array( $row['uid'], $row['app_id'], UserApplyHistoryModel::$apply_status_array[$row['status']], date("Y-m-d H:i:s", $row['created_at']), $row['remote_ip'], $idfa, $ios_version);
        }
        $file_name = "任务参与历史记录_" . date("YmdHis");
        $this->excelOutput($file_name, $title, $showlist);
    }


    #进行中的任务列表
    function underwayTaskAction()
    {
        $user_underway_model = new UserUnderWayModel();

        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['app_id'] = !empty($_REQUEST['app_id']) ? intval($_REQUEST['app_id']) : '0';

        $page_size = 20;
        $data = $user_underway_model->getListData($page, $page_size, $condition);
        if ($page == 1 && count($data) < 20) {
            $totalnum = count($data);
        } else {
            $totalnum = 1000;
        }
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '进行中的任务列表';
    }


    #用户收支记录
    function exchangeListAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $type = isset($_REQUEST['type']) ? trim($_REQUEST['type']) : 'all';
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        if ($type != 'all') {
            $condition['type'] = $type;
        }

        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), 1, date("Y"));
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';

        $exchange_obj = new UserExchangeModel();
        $page_size = 20;
        $data = $exchange_obj->getListData($page, $page_size, $condition);

        #分页处理
        $totalnum = $exchange_obj->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '用户收支记录';
    }


    #邀请列表
    function inviteListAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $condition['parent_id'] = !empty($_REQUEST['parent_id']) ? intval($_REQUEST['parent_id']) : '0';
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';

        $page_size = 20;
        $user_model = new UserModel();
        $data = $user_model->getListData($page, $page_size, $condition);

        #分页处理
        $totalnum = $user_model->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '邀请列表';
    }

    #邀请列表导出
    function inviteListOutputAction()
    {
        $condition = array();
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $condition['parent_id'] = !empty($_REQUEST['parent_id']) ? intval($_REQUEST['parent_id']) : '0';
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';

        $page_size = 10000;
        $user_model = new UserModel();
        $data = $user_model->getListData($page, $page_size, $condition);

        $title = array("邀请UID", "被邀请UID", "手机号", "注册时间", "IP", "获得分红");
        $showlist = array();
        foreach ($data as $k => $row) {
            $showlist[] = array($row['parent_id'], $row['uid'], $row['mobile'], date("Y-m-d H:i:s", $row['regtime']), $row['remote_ip'], $row['bonus_parant']);
        }
        $file_name = "邀请列表_" . date("YmdHis");
        $this->excelOutput($file_name, $title, $showlist);
    }

    /**
     * 用户微信头像审核列表
     */
    public function wechatavatarAction(){
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['code'] = !empty($_REQUEST['code']) ? intval($_REQUEST['code']) : '0';
        $condition['weixin_openid'] = !empty($_REQUEST['weixin_openid']) ? trim($_REQUEST['weixin_openid']) : '';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? $_REQUEST['start_date'] : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? $_REQUEST['end_date'] : '';

        $search = $condition;
        if (!empty($condition['start_date'])) {
            $search['start_date'] = strtotime($condition['start_date']);
        }
        if (!empty($condition['end_date'])) {
            $search['end_date'] = strtotime($condition['end_date']);
        }
        $page_size = 20;
        $weixin_code_model = new WeixinCodeModel();
        $data = $weixin_code_model->getListData($page, $page_size, $search);

        #分页处理
        $totalnum = $weixin_code_model->getListCount($search);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '微信头像绑定表';
    }


    #徒子徒孙页面
    function mobilesearchAction(){
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );
        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = $data = array();
        $err_url = '/manage/user/mobilesearch/';
        $condition['invite_uid'] = !empty($_REQUEST['invite_uid']) ? intval($_REQUEST['invite_uid']) : '0';
        if (!empty($condition['invite_uid'])) {
            $user_model = new  UserModel();
            $data = $user_model->getAdherents($condition['invite_uid']);

        }
        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '徒子徒孙列表';
    }



    #手机验证码查询页面
    function mobilecodeAction(){
        $condition = array();
        $mobile = !empty($_REQUEST['mobile']) ? intval($_REQUEST['mobile']) : '17621372073';

        $id = !empty($_REQUEST['id']) ? intval($_REQUEST['id']) : '0';
        if($id){
            $user_regmobile_model = new UserRegMobileModel();
            $mobile_info = $user_regmobile_model->getDateById($id);

            if(!$mobile_info){
                $this->set_flush_message('发生了错误，请重新尝试！');
                $this->redirect('/manage/user/mobilecode?mobile='.$mobile);
                return false;
            }


            $data['code'] = rand(1000,9999);
            $data['send_num'] = $mobile_info['send_num'] + 1;
            $data['updated_at'] = time();
            $user_regmobile_model->updateData($data,$id);
            $this->set_flush_message('机会给你了要珍惜');
            $this->redirect('/manage/user/mobilecode?mobile='.$mobile);
            return FALSE;
        }

        $showlist = array();
        if($mobile > 0){
            $user_regmobile_model = new UserRegMobileModel();
            $showlist = $user_regmobile_model->getMobileCode($mobile);
        }

        $this->_view->rowsets = $showlist;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '验证码查询';
    }


    #实名认证列表
    function identifyListAction()
    {
        $condition = array();
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['real_name'] = isset($_REQUEST['name']) ? addslashes(htmlspecialchars(trim($_REQUEST['name']))) : '';
        $condition['status'] = isset($_REQUEST['status']) ? ($_REQUEST['status'] != 'all' ? $_REQUEST['status'] : '0') : '1';  //1 已上传 2 已认证 -1 认证失败

        $page_size = 40;
        $user_identify_model = new UserIdentifyModel();
        $data = $user_identify_model->getListData($page, $page_size, $condition);

        #分页处理
        $totalnum = $user_identify_model->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '实名认证列表';
    }

    #审核操作实名认证
    function dealcheckidentifyAction(){

    }

    #支付宝待审核订单
    function waitCheckAlipayAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['realname'] = !empty($_REQUEST['realname']) ? addslashes(trim($_REQUEST['realname'])) : '';
        $condition['status'] = isset($_REQUEST['status']) ? $_REQUEST['status'] : '0';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0,0,0,date('m'),date('d')-30,date('Y'));;
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : $condition['start_date'] + 86400;
        $condition['type'] = 'alipay';

        $page_size = 40;
        $drawcash_history_obj = new DrawcashHistoryModel();
        $data = $drawcash_history_obj->getListData($page, $page_size, $condition);

        if(!empty($data)){
            foreach ($data as $k => $val) {
                $data[$k]['show_status'] = DrawcashHistoryModel::$draw_status[$val['status']];
            }
        }

        #分页处理
        list($totalnum, $total_date) = $drawcash_history_obj->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);


        $show_data = array(
            'data' => $data,
            'num' => $totalnum,
            'total' => $total_date
        );
        $this->_view->rowsets = $show_data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '支付宝待审核订单';
    }

    #人工支付宝审核操作
    function dealCheckAlipayAction()
    {
        $wx_drawcash_history_obj = new WeixinDrawcashHistoryModel();
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
        $curtime = time();
        if ($id > 0) {// 单个审核
            $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : 1;
            $order_data = $wx_drawcash_history_obj->getDataById($id);
            if (empty($order_data) || empty($order_data['uid']) || $order_data['status'] != 0) {
                $response_data['code'] = -1;
                $response_data['msg'] = '状态错误，请刷新后重试！';
                echo json_encode($response_data);
                exit();
            }
            if ($order_data['type'] != 'alipay') {
                $response_data['code'] = -1;
                $response_data['msg'] = '类型错误，请刷新后重试！';
                echo json_encode($response_data);
                exit();
            }
            $user_model = new UserModel();
            $user_identify_model = new UserIdentifyModel();
            $userinfo = $user_model->getUserInfoByUid($order_data['uid']);
            if ( (!empty($userinfo['alipay_account']) && $order_data['alipay_account'] != $userinfo['alipay_account']) || $userinfo['ban'] || $userinfo['frozen']) {
                #实名不匹配 自动拒绝提现
                $type = 0;
            }

            #用户被封禁的话，自动拒绝提现
            if(!empty($userinfo['ban'])) {
                $type = 0;
            }

            $draw_amt = $order_data['credits'];
            $amount = ($draw_amt)/100;
            if ($type == 1) {
                #审核通过
                // 个人的月累计提现校验
                $user_identify_info = $user_identify_model->getDataByUid($userinfo['uid']);
                if($user_identify_info['month_income'] >= UserIdentifyModel::MOST_MONEY &&  $user_identify_info['status'] < 2) {
                    $response_data['code'] = -1;
                    $response_data['msg'] = "未实名认证，当月累积提现有限制";
                    echo json_encode($response_data);
                    exit();
                }

                $alipay_req = array(
                    'out_biz_no' => $order_data['id'],
                    'payee_account' => $order_data['alipay_account'],
                    'payee_real_name' => $order_data['realname'],
                    'amount' => $amount,
                );
                $alipay_aop = new AlipayAop();
                $ret = $alipay_aop->TransToaccount($alipay_req['out_biz_no'], $alipay_req['payee_account'], $alipay_req['payee_real_name'], $alipay_req['amount']);

                if($ret['errcode'] != 2) {
                    $upd = array('status' => 1, 'updated_at' => $curtime, 'memo' => '支付宝提现失败'.$ret['errmsg']);
                    $wx_drawcash_history_obj->updateData($upd, $order_data['id']);

                    if($amount == 10){
                        $amount = $amount + 0.5;
                        $draw_amt = $draw_amt + 50;
                    }

                    /* 请求失败 - 补回用户金额 */
                    $user_model->changeBalance($userinfo['uid'], $draw_amt, true);


                    $exchange_model = new UserExchangeModel();
                    $exchage_data = array(
                        'uid' => $userinfo['uid'],
                        'type' => 'drawalipay_due',
                        'money' => $amount,
                        'remain' => floatval($userinfo['balance'] + $amount),
                        'memo' => '支付宝提现:' . $order_data['realname'] . ' 提现失败，补回金额:' . number_format($amount, 2) . '元',
                        'created_at' => time(),
                        'pic' => UserExchangeModel::BOOKS_ALIPAY_ICON,
                        'data_id' => $order_data['id']
                    );
                    $exchange_model->addData($exchage_data);

                    $response_data['code'] = -1;
                    $response_data['msg'] = $ret['errmsg'];
                    echo json_encode($response_data);
                    exit();
                }

                #支付吧提现成功

                $upd = array('status' => 2, 'updated_at' => $curtime, 'memo' => '提现成功','trans_num' => $ret['order_id']);
                $wx_drawcash_history_obj->updateData($upd, $order_data['id']);


                #写入user 实名
                $upd_user = array();
                if (empty($userinfo['alipay_account'])) {
                    $upd_user['alipay_account'] = $order_data['alipay_account'];
                }
                if (empty($userinfo['real_name'])) {
                    $upd_user['real_name'] = $order_data['realname'];
                }
                if(!empty($upd_user)) {
                    $user_model->updateData($upd_user, $userinfo['uid']);
                }
                // 修改月累积提现额度
                $cur_month_time = mktime(0,0,0, date('m'), 1, date('Y'));
                $upd_idt = array(
                    'updated_at' => $curtime,
                );
                if($user_identify_info['updated_at'] >= $cur_month_time) {
                    #本月累加
                    $upd_idt['month_income'] = $user_identify_info['month_income'] + ($draw_amt/100);
                } else {
                    #清空历史
                    $upd_idt['month_income'] =  ($draw_amt/100);
                }
                $user_identify_model->updateData($upd_idt, $userinfo['uid']);

            } else {
                #审核拒绝
                $upd = array('status' => 3, 'updated_at' => $curtime, 'memo' => '审核拒绝');
                $wx_drawcash_history_obj->updateData($upd, $order_data['id']);

                if($amount == 10){
                    $draw_amt = $draw_amt + 50;
                }

                /* 请求失败 - 补回用户金额 */
                $user_model->changeBalance($userinfo['uid'], $draw_amt, true);
                $amount = ($draw_amt)/100;
                $exchange_model = new UserExchangeModel();
                $exchage_data = array(
                    'uid' => $userinfo['uid'],
                    'type' => 'drawalipay_due',
                    'money' => $amount,
                    'remain' => floatval($userinfo['balance'] + $amount),
                    'memo' => '支付宝提现:' . $order_data['realname'] . ' 提现失败，补回金额:' . number_format($amount, 2) . '元',
                    'created_at' => time(),
                    'pic' => UserExchangeModel::BOOKS_ALIPAY_ICON,
                    'data_id' => $order_data['id']
                );
                $exchange_model->addData($exchage_data);
            }

        } else {
            $this->set_flush_message('批量审核尚未完成！');
            $this->redirect('/manage/user/waitcheckalipay/');
            exit();
        }
        $response_data['code'] = 1;
        $response_data['msg'] = '审核操作已完成';
        echo json_encode($response_data);
        exit();
    }


    /***
     * 微信提现列表
     */
    #待审核订单列表
    function waitCheckOrderAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['realname'] = !empty($_REQUEST['realname']) ? addslashes(trim($_REQUEST['realname'])) : '';
        $condition['status'] = isset($_REQUEST['status']) ? $_REQUEST['status'] : '0';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0,0,0,date('m'),date('d')-30,date('Y'));;
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : $condition['start_date'] + 86400;
        $condition['type'] = 'weixin';

        $page_size = 40;
        $drawcash_history_obj = new DrawcashHistoryModel();
        $data = $drawcash_history_obj->getListData($page, $page_size, $condition);

        if(!empty($data)){
            foreach ($data as $k => $val) {
                $data[$k]['show_status'] = DrawcashHistoryModel::$draw_status[$val['status']];
            }
        }

        #分页处理
        list($totalnum, $total_date) = $drawcash_history_obj->getListCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $show_data = array(
            'data' => $data,
            'num' => $totalnum,
            'total' => $total_date
        );
        $this->_view->rowsets = $show_data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '待审核订单列表';
    }



    #人工审核操作
    function dealCheckOrderAction()
    {
        $wx_drawcash_history_obj = new WeixinDrawcashHistoryModel();
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
        $curtime = time();
        if ($id > 0) {// 单个审核
            $remote_ip = $wx_drawcash_history_obj->getIP();
            $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : 1;
            $order_data = $wx_drawcash_history_obj->getDataById($id);
            if (empty($order_data) || empty($order_data['uid']) || $order_data['status'] != 0) {
                $response_data['code'] = -1;
                $response_data['msg'] = '状态错误，请刷新后重试！';
                echo json_encode($response_data);
                exit();
            }

            $user_model = new UserModel();
            $user_identify_model = new UserIdentifyModel();
            $userinfo = $user_model->getUserInfoByUid($order_data['uid']);

            if ((!empty($userinfo['real_name']) && $order_data['realname'] != $userinfo['real_name'])) {
                #实名不匹配 自动拒绝提现
                $type = 0;
            }
            #用户被封禁的话，自动拒绝提现
            if(!empty($userinfo['ban']) || !empty($userinfo['frozen'])) {
                $type = 0;
            }

            $draw_amt = $order_data['credits'];
            if ($type == 1) {
                #审核通过
                if(empty($userinfo['weixin'])) {
                    $response_data['code'] = -1;
                    $response_data['msg'] = "没有绑定微信";
                    echo json_encode($response_data);
                    exit();
                }
                // 个人的月累计提现校验
                $user_identify_info = $user_identify_model->getDataByUid($userinfo['uid']);
                if($user_identify_info['month_income'] >= UserIdentifyModel::MOST_MONEY &&  $user_identify_info['status'] < 2) {
                    $response_data['code'] = -1;
                    $response_data['msg'] = "未实名认证，当月累积提现有限制";
                    echo json_encode($response_data);
                    exit();
                }

                $drawinfo = array(
                    'uid' => $order_data['uid'],
                    'wx_draw_history_id' => $order_data['id'],
                    'realname' => $order_data['realname'],
                    'credits' => $order_data['credits'],
                    'weixin' => $userinfo['weixin'],
                    'remote_ip' => $remote_ip,
                );
                $req_params = WeixinWithdraw::genReqParams($drawinfo);
                $result = WeixinWithdraw::responseMsg($req_params);

                #微信返回失败
                if (empty($result['errcode']) || $result['errcode'] != 2) {
                    $upd = array('status' => 1, 'updated_at' => $curtime, 'memo' => '微信接口失败'.$result['errmsg']);
                    $wx_drawcash_history_obj->updateData($upd, $order_data['id']);

                    /* 请求失败 - 补回用户金额 */
                    $user_model->changeBalance($userinfo['uid'], $draw_amt, true);

                    $amount = ($draw_amt)/100;
                    $exchange_model = new UserExchangeModel();
                    $exchage_data = array(
                        'uid' => $userinfo['uid'],
                        'type' => 'drawwechat_due',
                        'money' => $amount,
                        'remain' => floatval($userinfo['balance'] + $amount),
                        'memo' => '微信提现:' . $order_data['realname'] . ' 提现失败，补回金额:' . number_format($amount, 2) . '元',
                        'created_at' => time(),
                        'pic' => UserExchangeModel::BOOKS_CASH_ICON,
                        'data_id' => $order_data['id']
                    );
                    $exchange_model->addData($exchage_data);

                    $response_data['code'] = -1;
                    $response_data['msg'] = $result['errmsg'];
                    echo json_encode($response_data);
                    exit();
                }

                #微信提现成功
                $upd = array('status' => 2, 'updated_at' => $curtime, 'memo' => '提现成功','trans_num' => $result['succdata']['payment_no']);
                $wx_drawcash_history_obj->updateData($upd, $order_data['id']);

                #写入user 实名
                if (empty($userinfo['real_name'])) {
                    $upd_user = array('real_name' => $order_data['realname']);
                    $user_model->updateData($upd_user, $userinfo['uid']);
                }

                // 修改月累积提现额度
                $cur_month_time = mktime(0,0,0, date('m'), 1, date('Y'));
                $upd_idt = array(
                    'updated_at' => $curtime,
                );
                if($user_identify_info['updated_at'] >= $cur_month_time) {
                    #本月累加
                    $upd_idt['month_income'] = $user_identify_info['month_income'] + ($draw_amt/100);
                } else {
                    #清空历史
                    $upd_idt['month_income'] =  ($draw_amt/100);
                }
                $user_identify_model->updateData($upd_idt, $userinfo['uid']);

            } else {
                #审核拒绝
                $upd = array('status' => 3, 'updated_at' => $curtime, 'memo' => '审核拒绝');
                $wx_drawcash_history_obj->updateData($upd, $order_data['id']);

                /* 请求失败 - 补回用户金额 */
                $user_model->changeBalance($userinfo['uid'], $draw_amt, true);
                $amount = ($draw_amt)/100;
                $exchange_model = new UserExchangeModel();
                $exchage_data = array(
                    'uid' => $userinfo['uid'],
                    'type' => 'drawwechat_due',
                    'money' => $amount,
                    'remain' => floatval($userinfo['balance'] + $amount),
                    'memo' => '微信提现:' . $order_data['realname'] . ' 提现失败，补回金额:' . number_format($amount, 2) . '元',
                    'created_at' => time(),
                    'pic' => UserExchangeModel::BOOKS_CASH_ICON,
                    'data_id' => $order_data['id']
                );
                $exchange_model->addData($exchage_data);
            }

        } else {// 多选通过审核
            $this->set_flush_message('批量审核尚未完成！');
            $this->redirect('/manage/user/waitcheckorder/');
            exit();
        }

        $response_data['code'] = 1;
        $response_data['msg'] = '审核操作已完成';
        echo json_encode($response_data);
        exit();
    }


#审核白名单
    function whiteListAction()
    {
        $white_list = isset($_REQUEST['white_list']) ? trim($_REQUEST['white_list']) : '';
        $curtime = time();

        if ($this->getRequest()->isPost()) {
            $keyword_arr = explode("\r\n", $white_list);
            print_r($keyword_arr);
            $value = array();
            foreach($keyword_arr as $val){
                $value[] = explode("	", $val);
            }
            print_r($value);
            die;

            $this->set_flush_message("添加白名单成功");
            $this->redirect($this->referer());
            return FALSE;
        }
        $this->_view->white_list = '';
        $this->_layout->meta_title = '添加白名单';
    }

    /***
     * 问题用户列表
     */
    function problemAction(){
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['mobile'] = !empty($_REQUEST['mobile']) ? trim($_REQUEST['mobile']) : '';
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['wifiname'] = !empty($_REQUEST['wifiname']) ? trim($_REQUEST['wifiname']) : '';
        $condition['start_date'] = !empty($_REQUEST['start_date']) ? $_REQUEST['start_date'] : '';
        $condition['end_date'] = !empty($_REQUEST['end_date']) ? $_REQUEST['end_date'] : '';
        $condition['ban'] = isset($_REQUEST['ban']) ? intval($_REQUEST['ban']) : '0';
        $condition['frozen'] = isset($_REQUEST['frozen']) ? intval($_REQUEST['frozen']) : '0';
        $condition['address'] = isset($_REQUEST['address']) ? trim($_REQUEST['address']) : '';
        $condition['sort'] = isset($_REQUEST['sort']) ? trim($_REQUEST['sort']) : '';
        $condition['sign'] = isset($_REQUEST['sign']) ? $_REQUEST['sign'] : '';
        $condition['birthday'] = isset($_REQUEST['birthday']) ? intval($_REQUEST['birthday']) : '';
        $condition['udid_from'] = isset($_REQUEST['udid_from']) ? intval($_REQUEST['udid_from']) : '';

        $search = $condition;
        if (!empty($condition['start_date'])) {
            $search['start_date'] = strtotime($condition['start_date']);
        }
        if (!empty($condition['end_date'])) {
            $search['end_date'] = strtotime($condition['end_date']);
        }
        $page_size = 20;
        $user_model = new UserModel();
        $data = $user_model->getProblemData($page, $page_size, $search);


        #分页处理
        $totalnum = $user_model->getProblemCount($search);
        if ($page == 1 && count($data) < 20) {
            $totalnum = count($data);
        }

        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '问题用户列表';
    }
}
?>